package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.dialog.v3.CustomDialog;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/19
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class DrawAmountPresenter {
    public AppCompatActivity me;

    public DrawAmountPresenter(AppCompatActivity me) {
        this.me = me;
    }

    /**
     * 领取奖金
     * */
    public void drawAmount(String token){
        Model.getInstance().getUserApi().drawAmount(token)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null){
                            return;
                        }
                        ToastUtil.show(map.get("message"));
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }
    /**
     * 是否领取金额
     * */
    public void isLing(final String token){
        Model.getInstance().getUserApi().isLing(token)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null){
                            return;
                        }
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        if (data != null){
                           if (data.get("is_draw_amount").equals("1")){
                               CustomDialog.build(me, R.layout.activity_red_envelopes, new CustomDialog.OnBindView() {
                                   @Override
                                   public void onBind(final CustomDialog dialog, View v) {
                                       ImageView image_bottom = v.findViewById(R.id.image_bottom);
                                       TextView text_money = v.findViewById(R.id.text_money);
                                       text_money.setText(data.get("money")+"元");
                                       ImageView image_finish = v.findViewById(R.id.image_finish);
                                       image_finish.setOnClickListener(new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               dialog.doDismiss();
                                           }
                                       });
                                       image_bottom.setOnClickListener(new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               drawAmount(token);
                                               dialog.doDismiss();
                                           }
                                       });
                                   }
                               }).setCancelable(false).show();
                           }
                        }
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }


}
